<template>
    <!-- ChildrenDict对话框 -->
    <el-dialog
      :title="!addChildrenDictForm.dictId ? '新增' : '修改'"
      :visible.sync="addChildrenDictdialog"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="40%"
      >
      <el-form
        :model="addChildrenDictForm"
        :rules="addChildrenDictFormRules"
        ref="addChildrenDictFormRef"
        :close-on-click-modal="false"
        label-width="80px"
        :size="size">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属分组" prop="dictTypeId"
              style="margin-left: 0; float: left;">
              <el-select
                v-model="addChildrenDictForm.dictTypeId"
                :disabled="disabled"
                @change="changeDictTypeId"
                placeholder="请选择分组">
                <el-option
                  v-for="item in options"
                  :key="item.dictTypeId"
                  :label="item.dictTypeName"
                  :value="item.dictTypeId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="addChildrenDictForm.dictName"></el-input>
        </el-form-item>
        <el-form-item label="字典编码" prop="code">
          <el-input v-model="addChildrenDictForm.code"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="10">
            <el-form-item label="排序号"
              style="margin-left: 0; float: left;">
              <el-input-number
                v-model="addChildrenDictForm.sort"
                :min="1"
                :max="100000"
                label="排序号"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="状态"
              style="margin-left: 0; float: left;">
              <el-switch
                v-model="addChildrenDictForm.status"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              label="默认字典"
              prop="isDefault"
              style="margin-left: 0; float: left;">
              <el-switch
                v-model="addChildrenDictForm.isDefault">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" >
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="addChildrenDictForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :size="size" @click="addChildrenDictdialog = false">取 消</el-button>
        <el-button :size="size" type="primary" @click="submitChildrenDict">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import {
  postChildrenDict,
  editChildrenDict,
  getParentDictTable,
  getDictDataById
} from '@/api/auth/dict.js'
export default {
  props: {
    dictTypeId: {
      type: String
    }
  },
  data() {
    return {
      disabled: false,
      size: 'small',
      addChildrenDictdialog: false,
      addChildrenDictForm: {
        dictName: '',
        dictId: '',
        code: '',
        status: '1',
        description: '',
        dictTypeId: '',
        sort: 1,
        isDefault: 0
      },
      addChildrenDictFormRules: {
        dictTypeId: [
          { required: true, message: '请输入分组名称', trigger: 'blur' }
        ],
        dictName: [
          { required: true, message: '请输入分组名称', trigger: 'blur' },
          { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入分组编码', trigger: 'blur' },
          { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
        ],
        description: [
          { min: 0, max: 255, message: '长度在 0 到 255 个字符', trigger: 'blur' }
        ]
      },
      value: '',
      options: [],
      id: ''
    }
  },
  created() {
    this.getTable()
  },
  methods: {
    // 获取分组数据
    getTable() {
      getParentDictTable().then((res) => {
        this.options = res.data
      })
    },
    init(val, dictTypeId) {
      // 清空数据
      this.clearChildrenForm()
      this.addChildrenDictForm.dictTypeId = dictTypeId
      this.getTable()
      this.id = val.dictId
      if (JSON.stringify(val) !== '{}'){
        // 修改
        getDictDataById(val.dictId).then(res => {
          if (res.code === 200) this.addChildrenDictForm = res.data
        })
      }
      this.addChildrenDictdialog = true
    },
    clearChildrenForm() {
      this.addChildrenDictForm.dictId = ''
      this.addChildrenDictForm.dictTypeId = ''
      this.addChildrenDictForm.dictName = ''
      this.addChildrenDictForm.description = ''
      this.addChildrenDictForm.code = ''
      this.addChildrenDictForm.status = '1'
      this.addChildrenDictForm.sort = 1
      this.addChildrenDictForm.isDefault = 0
    },
    changeDictTypeId() {
      this.$forceUpdate()
    },
    // 提交表单操作
    submitChildrenDict() {
      this.$refs.addChildrenDictFormRef.validate(valid => {
        if (!valid) return
        this.$confirm('确认提交吗？', '提示', {}).then(() => {
          if (!this.addChildrenDictForm.dictId) {
            const data = {
              dictName: this.addChildrenDictForm.dictName,
              dictId: this.addChildrenDictForm.dictId,
              code: this.addChildrenDictForm.code,
              status: this.addChildrenDictForm.status,
              description: this.addChildrenDictForm.description,
              dictTypeId: this.addChildrenDictForm.dictTypeId,
              sort: this.addChildrenDictForm.sort,
              isDefault: this.addChildrenDictForm.isDefault
            }
            postChildrenDict(data).then(res => {
              const dictTypeid = this.addChildrenDictForm.dictTypeId
              this.$emit('refreshChildrenDataList', dictTypeid)
              this.addChildrenDictdialog = false
            })
          } else {
            const val = {
              id: this.addChildrenDictForm.dictId,
              dictName: this.addChildrenDictForm.dictName,
              code: this.addChildrenDictForm.code,
              status: this.addChildrenDictForm.status,
              description: this.addChildrenDictForm.description,
              dictTypeId: this.addChildrenDictForm.dictTypeId,
              sort: this.addChildrenDictForm.sort,
              isDefault: this.addChildrenDictForm.isDefault
            }
            editChildrenDict(val).then(res => {
              this.$emit('refreshChildrenDataList', this.addChildrenDictForm.dictTypeId)
              this.addChildrenDictdialog = false
            })
          }
        })
      })
    }
  }
}
</script>

<style>
</style>
